#!/bin/sh

setup_mysql()
{
	local mnt_point=$1
        if [ "$os" = "centos" -o "$os" = "debian" ]
        then
		systemctl stop mariadb.service
	fi
		

	# fix "Neither host 'lkp-bdw-ex1' nor 'localhost'
	# could be looked up with '/usr/sbin/resolveip'"
	# from mysql_install_db on Debian.
	[ ! -x /usr/sbin/resolveip ] && \
	ln -s $(which resolveip) /usr/sbin/resolveip
	
        if [ "$os" = "openeuler" ]
        then
                echo "max_connections=5000" >> /etc/my.cnf.d/mysql-server.cnf
                chown -R mysql:mysql /var/lib/mysql
                service mysqld start
        elif [ "$os" = "centos" ]
        then
                chown -R mysql:mysql /var/lib/mysql
                service mariadb.service start
        fi	

	mkdir -p $mnt_point/mysql
        chown -R mysql:mysql $mnt_point/mysql

        ISmysql_install_db="YES"
        mysql_install_db --version > /dev/null 2>&1 || ISmysql_install_db="NO"
        if [ $ISmysql_install_db = "YES" ]
        then
		mysql_install_db --datadir="$mnt_point/mysql" --user=mysql
        else
                mysqld --initialize --datadir="$mnt_point/mysql" --user=mysql --initialize-insecure
        fi

	# fix "FATAL: error 1040: Too many connections"
	#local max_conn=100
	#if [ -n "$nr_threads" ] && [ "$nr_threads" -gt 100 ]; then
	#	max_conn=$((nr_threads * 2))
	#elif [ -n "$nr_task" ] && [ "$nr_task" -gt 100 ]; then
	#	max_conn=$((nr_task * 2))
	#fi
        local max_conn=4000
        if [ -n "$nr_threads" ] && [ "$nr_threads" -gt "$max_conn" ]; then
                max_conn=$((nr_threads * 2))
        elif [ -n "$nr_task" ] && [ "$nr_task" -gt "$max_conn" ]; then
                max_conn=$((nr_task * 2))
        fi



	# /usr/libexec/mysqld is used on RHEL/Centos
	# /usr/bin/mysql is used on Debian/Ubuntu
	# /usr/bin/haswell/mysql is used on Clear Linux
	local exec_mysqld='mysqld'
	mysqld --version >/dev/null 2>&1 || exec_mysqld='/usr/libexec/mysqld'
	$exec_mysqld --datadir="$mnt_point/mysql" --max-connections="$max_conn" --skip-grant-tables --user=mysql &
}

[ -n "$mount_points" ] || { echo "empty mount_points, disk failed?" >&2; exit; }

mnt_point="${mount_points%% *}"

if [ "$os" = "centos" ]
then
        echo "ZGS20230817: yum install -y dpkg sysbench psmisc"
        yum install -y epel-release
        yum repolist
        yum install -y dpkg-dev
        yum install -y psmisc
        yum install -y git
        yum install -y automake 1.10.x
        yum install -y libtool
        mkdir /root/sysbench
        cd /root/sysbench
        git clone https://gitee.com/zhai-gaoshou/sysbench-akopytov-zgs.git
        cd sysbench-akopytov-zgs
        ./autogen.sh
        ./configure
        make
        make install
        ln -s /usr/local/share/sysbench /usr/share/sysbench
fi

[ -f /var/run/mysqld/mysqld.sock ] || {
	# ERROR: There's not enough space in /var/lib/mysql/
	# dpkg: error processing archive /opt/deb/mariadb-server-10.3_1%3a10.3.23-0+deb10u1_amd64.deb (--install):
	#   new mariadb-server-10.3 package pre-installation script subprocess returned error exit status 1
        [ "$os" = "centos" ] && yum install -y dpkg
        [ -d /var/lib/mysql ] || mkdir -p /var/lib/mysql

	mkdir -p $mnt_point/var_lib_mysql
	mount --bind $mnt_point/var_lib_mysql /var/lib/mysql
        if [ "$os" = "debian" ]
        then
		dpkg --force-all -i /opt/deb/mariadb*
	fi
}
setup_mysql "$mnt_point"
